package com.icim.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.icim.core.service.IAlarmSenderService;
import com.icim.core.tx.Amssend.AmssenderI;
import com.icim.dao.BisDataDao;
import com.icim.entity.Bis_data;
import com.icim.util.HttpClientUtil;
import com.icim.util.XmlConvertUtil;

@Service
public class AlarmSenderService implements IAlarmSenderService {

    private Logger logger = Logger.getLogger(AlarmSenderService.class);

    @Resource
    HttpClientUtil httpClientUtil;
    
    @Resource
    BisDataDao bisDataDao;

    public void sendAlarm(String mailText, String mailToAdds) {
        String subject = "资料外送系统出现错误";
        sendToAMS(mailText, subject, mailToAdds);
    }
    
    public void sendAlarm1(String cus_id, String mailText){
    	String mailToAdds="";
    	String mailSubject=cus_id+"客户数据回传失败告警，请尽快处理！";
    	List<Bis_data> addList=bisDataDao.listData("XMSM", cus_id, "","","","","","","");
    	for(int k=0; k<addList.size(); k++){
			if(addList.get(k).getData_ext() != null && ! "".equals(addList.get(k).getData_ext())){
					mailToAdds = mailToAdds + addList.get(k).getData_ext() + "," ;
			}
		}
    	if(!"".equals(mailToAdds)){
    		sendToAMS(mailText,mailSubject,mailToAdds);
    	}
    	
    	
    }

    private void sendToAMS(String mailText, String subject, String mailToAdds) {
        AmssenderI inTrx = new AmssenderI();
        inTrx.setMail_subject(subject);
        inTrx.setMail_text(mailText);
        inTrx.setTrx_id("AMSSENDER");
        inTrx.setType_id("I");
        inTrx.setAction_flg("M");
        inTrx.setMail_toAdds(mailToAdds);
        String outMesg = httpClientUtil.postHttp(XmlConvertUtil.obj2Xml(inTrx, "transaction"));

        if (outMesg.contains("0000000")) {
            logger.info(outMesg);
        } else {
            logger.error(outMesg);
        }
    }
}
